<?php

/**
 * categories actions.
 *
 * @package    sf_sandbox
 * @subpackage categories
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 9301 2008-05-27 01:08:46Z dwhittle $
 */
class categoriesActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex($request)
  {
    $c = new Criteria();
    $c->add(CategoriesPeer::PARENT_ID, null, Criteria::ISNULL);
    $this->categories = CategoriesPeer::doSelect($c);
    
    $this->getUser()->setAttribute('parent', '');
    
  }
    
  public function executeSubcategories($request)
  {
	  $c = new Criteria();
	  $c->add(CategoriesPeer::PARENT_ID, $request->getParameter('parent'));
	  $this->categories = CategoriesPeer::doSelect($c);
	  
	  $this->products = array();
	  if($this->categories)
	  {
	  	$this->getUser()->setAttribute('parent', $request->getParameter('parent'));
	  	foreach($this->categories as $categorie)
	  	{
	  		$cpi = new Criteria();
		    $cpi->add(ProductsToCategoriesPeer::CATEGORIES_ID, $categorie->getCategoriesId());
		    $cpi->addJoin(ProductsToCategoriesPeer::PRODUCTS_ID, ProductsPeer::PRODUCTS_ID);
		    $this->products = array_merge($this->products, ProductsPeer::doselect($cpi));
	  	}
	  }
	  
	  $cpi = new Criteria();
	  $cpi->add(ProductsToCategoriesPeer::CATEGORIES_ID, $request->getParameter('parent'));
	  $cpi->addJoin(ProductsToCategoriesPeer::PRODUCTS_ID, ProductsPeer::PRODUCTS_ID);
	  $this->products = array_merge($this->products, ProductsPeer::doselect($cpi));
  }
  
  public function executeProducts($request)
  {
    $c = new Criteria();
    $c->add(ProductsPeer::PRODUCTS_ID, $request->getParameter('products'));
    $this->product = ProductsPeer::doSelectOne($c);
  }
}